//
//  TopicTableViewCell.m
//  Zaker
//
//  Created by dllo on 16/3/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicTableViewCell.h"

@interface TopicTableViewCell ()

@property (nonatomic, retain) BaseView *bigView;
@property (nonatomic, retain) UIImageView *ima;
@property (nonatomic, retain) UILabel *name;
@property (nonatomic, retain) UIImageView *photo;
@property (nonatomic, retain) BaseLabel *title;
@property (nonatomic, retain) BaseLabel *follow;
@property (nonatomic, retain) UILabel *kind;
@property (nonatomic, retain) BaseButton *add;
@property (nonatomic, strong) CoreDataManager *manager;

@end

@implementation TopicTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.manager = [CoreDataManager defaults];
        
        self.bigView = [[BaseView alloc] init];
        [self.contentView addSubview:_bigView];
        _bigView.layer.borderColor = [UIColor colorWithWhite:0.702 alpha:1.000].CGColor;
        _bigView.layer.borderWidth = 1;
        
        self.ima = [[UIImageView alloc] init];
        [self.bigView addSubview:_ima];
        
        self.photo = [[UIImageView alloc] init];
        [self.contentView addSubview:_photo];
        
        self.name = [[UILabel alloc] init];
        [self.contentView addSubview:_name];
        _name.textColor = [UIColor whiteColor];
        
        self.title = [[BaseLabel alloc] init];
        [self.bigView addSubview:_title];
        self.title.numberOfLines = 2;
        
        self.follow = [[BaseLabel alloc] init];
        [self.bigView addSubview:_follow];
        _follow.font = [UIFont systemFontOfSize:13];
        
        self.kind = [[UILabel alloc] init];
        [self.bigView addSubview:_kind];
        _kind.textColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
        _kind.font = [UIFont systemFontOfSize:13];
        
        self.add = [[BaseButton alloc] init];
        [self.bigView addSubview:_add];
        [_add setTitle:@"+ 关注" forState:UIControlStateNormal];
        [_add setTitle:@"√已关注" forState:UIControlStateSelected];
        [_add setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_add setTitleColor:[UIColor colorWithWhite:0.400 alpha:1.000] forState:UIControlStateSelected];
        _add.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.add addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFollow) name:@"changeFollow" object:nil];
    }
    return self;
}

- (void)changeFollow
{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    NSArray *arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSMutableArray *arrT = [NSMutableArray array];
    for (Topic *m in arr) {
        [arrT addObject:m.title];
        NSLog(@"%@", m.title);
    }
    NSLog(@"%@", arrT);
    if ([arrT containsObject:_model.title]) {
        _add.selected = YES;
    } else {
        _add.selected = NO;
    }
}

#pragma mark - 收藏按钮
- (void)add:(BaseButton *)button
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Topic" inManagedObjectContext:self.manager.managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    NSArray *arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    button.selected = !button.selected;
    if (button.selected) {
        for (Topic *m in arr) {
            if ([m.title isEqualToString:_model.title]) {
                return;
            }
        }
        Topic *top = [[Topic alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
        top.expertId = _model.expertId;
        top.alias = _model.alias;
        top.picurl = _model.picurl;
        top.classification = _model.classification;
        top.name = _model.name;
        top.headpicurl = _model.headpicurl;
        top.title = _model.title;
        top.questionCount = _model.questionCount;
        top.answerCount = _model.answerCount;
        top.expertDescription = _model.expertDescription;
//        for (Topic *m in arr) {
//            if ([m.title isEqualToString:_model.title]) {
//                return;
//            }
//        }
//        Topic *top = [[Topic alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
        [self.manager saveContext];
    } else {
        for (Topic *m in arr) {
            if ([m.title isEqualToString:_model.title]) {
                [self.manager.managedObjectContext deleteObject:m];
            }
        }
        [self.manager saveContext];
    }
    //NSLog(@"数据库收藏:%@", arr);
    NSLog(@"url === %@", [self.manager applicationDocumentsDirectory]);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
//    NSError *error;
//    NSArray *arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
//    NSMutableArray *arrT = [NSMutableArray array];
//    for (Topic *m in arr) {
//        [arrT addObject:m.title];
//        NSLog(@"%@", m.title);
//    }
//    NSLog(@"%@", arrT);
//    if ([arrT containsObject:_model.title]) {
//        _add.selected = YES;
//    } else {
//        _add.selected = NO;
//    }
    
    _bigView.frame = CGRectMake(10, 5, SCREEN_WIDTH - 20, self.contentView.frame.size.height - 10);
    
    _ima.frame = CGRectMake(0, 0, _bigView.frame.size.width, _bigView.frame.size.height / 3 * 2);
    
    _photo.frame = CGRectMake(20, _ima.frame.size.height / 4 * 3, _ima.frame.size.width / 9, _ima.frame.size.width / 9);
    _photo.layer.cornerRadius = _photo.frame.size.width / 2;
    _photo.clipsToBounds = YES;
    
    _name.frame = CGRectMake(_photo.frame.origin.x + _photo.frame.size.width + 5, 0,self.contentView.frame.size.width - _photo.frame.size.width, _photo.frame.size.height / 2);
    _name.center = CGPointMake(_name.center.x, _photo.center.y);
    
    _title.frame = CGRectMake(10, _ima.frame.size.height + 10, SCREEN_WIDTH - 40, _ima.frame.size.height / 4);
    
    _follow.frame = CGRectMake(10, _title.frame.origin.y + _title.frame.size.height + 10, 75, _title.frame.size.height / 2);
    
    _kind.frame = CGRectMake(_follow.frame.origin.x + _follow.frame.size.width, _follow.frame.origin.y, 40, _follow.frame.size.height);
    
    _add.frame = CGRectMake(SCREEN_WIDTH - 30 - 60, _follow.frame.origin.y, 60, _follow.frame.size.height);
    _add.layer.cornerRadius = 10;
    _add.layer.borderColor = [UIColor colorWithWhite:0.902 alpha:1.000].CGColor;
    _add.layer.borderWidth = 1;
}

- (void)setModel:(TopicModel *)model
{
    if (_model != model) {
        _model = model;
    }
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Topic"];
    NSError *error;
    NSArray *arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSMutableArray *arrT = [NSMutableArray array];
    for (Topic *m in arr) {
        [arrT addObject:m.title];
        NSLog(@"%@", m.title);
    }
    NSLog(@"%@", arrT);
    if ([arrT containsObject:_model.title]) {
        _add.selected = YES;
    } else {
        _add.selected = NO;
    }
    
    [_ima sd_setImageWithURL:[NSURL URLWithString:model.picurl] placeholderImage:[UIImage imageNamed:@"User_Profiles_bg"]];
    [_photo sd_setImageWithURL:[NSURL URLWithString:model.headpicurl] placeholderImage:[UIImage imageNamed:@"User_Profiles_bg"]];
    _name.text = model.name;
    _title.text = model.alias;
    if (model.concernCount.intValue < 10000) {
        _follow.text = [NSString stringWithFormat:@"%@关注 |", model.concernCount];
    } else {
        _follow.text = [NSString stringWithFormat:@"%.1f万关注 |", model.concernCount.floatValue / 10000];
    }
    _kind.text = model.classification;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
